﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
/*
    2012-2-17 David检查正常
 */
namespace Benefit.Service.Sys
{
    /// <summary>
    /// 为交易员配置小组
    /// </summary>
    public class ServiceGroupOperators : Benefit.Interface.Sys.IGroupOperators
    {
        Benefit.DB.DBManager db = null;
        public ServiceGroupOperators(Benefit.DB.DBManager db)
        {
            this.db = db;
        }
        public void AddGroupOperators(int groupId,int operatorId)
        {
            Models.Sys.GroupOperators dgos = new Models.Sys.GroupOperators();
            dgos.GroupId = groupId;
            dgos.OperatorId = operatorId;
            db.GroupOperators.Add(dgos);
            db.SaveChanges();
        }

        public List<Models.Sys.GroupOperators> GetList()
        {
            var query = db.GroupOperators.ToList();
            return query;
        }

        /// <summary>
        /// 在网页中配置账户和操作员关系
        /// </summary>
        /// <param name="oas"></param>
        public void ResetOperatorGroups(List<Models.Sys.GroupOperators> gas)
        {
            foreach (Models.Sys.GroupOperators account in gas)
            {
                var query = db.GroupOperators.Where(a => a.OperatorId == account.OperatorId);
                if (query.Count() > 0)
                {
                    Models.Sys.GroupOperators a = query.First();
                    a.GroupId = account.GroupId;
                }
                else
                {
                    db.GroupOperators.Add(account);
                }
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 得到一个组的所有组员
        /// </summary>
        /// <param name="groupid"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public List<Models.Sys.GroupOperators> GetGroupOperators(int groupid)
        {
            return db.GroupOperators.Where(a => a.GroupId == groupid).ToList();
        }
    }
}